home *** CD-ROM | disk | FTP | other *** search
/ LiquidLibrary 2005 February / LiquidLibrary 2005 February - Disc 1.iso / pc / Portfolio Browser / Filters / PDF / LIB / gs_ll3.ps < prev    next >
Text File  |  2003-01-03  |  15KB  |  459 lines

  1. %    Copyright (C) 1997, 2000 Aladdin Enterprises.  All rights reserved.
  2. % This software is licensed to a single customer by Artifex Software Inc.
  3. % under the terms of a specific OEM agreement.
  4.  
  5. % $RCSfile$ $Revision$
  6. % Initialization file for PostScript LanguageLevel 3 functions.
  7. % This file must be loaded after gs_lev2.ps and gs_res.ps.
  8. % These definitions go into ll3dict or various ProcSets.
  9. % NOTE: the interpreter creates ll3dict.
  10.  
  11. ll3dict begin
  12.  
  13. % We need LanguageLevel 2 or higher in order to have setuserparams and
  14. % defineresource.
  15. languagelevel dup 2 max .setlanguagelevel
  16.  
  17. % ------ Idiom recognition ------ %
  18.  
  19. /IdiomRecognition false .definepsuserparam
  20.  
  21. % Modify `bind' to apply idiom recognition afterwards.
  22. /.bindscratch 128 string def
  23. % Do the right thing if NOBIND or DELAYBIND is in effect.
  24. % Note also that since this definition of `bind' may get bound in,
  25. % it has to function properly even at lower language levels,
  26. % where IdiomRecognition may not be defined.
  27. /bind load /.bind load ne
  28. /bind {        % <proc> bind <proc'>
  29.   //.bind currentuserparams /IdiomRecognition
  30.   .knownget not { false } if {
  31.     (*) {
  32.       /IdiomSet findresource
  33.       false exch {
  34.         % Stack: proc false dummykey [template substitute]
  35.     exch pop dup 1 get exch 0 get
  36.         % Stack: proc false substitute template
  37.     3 index .eqproc {
  38.       2 index gcheck 1 index gcheck not and {
  39.         pop
  40.       } {
  41.         3 -1 roll pop exch not exit
  42.       } ifelse
  43.     } {
  44.       pop
  45.     } ifelse
  46.       } forall { exit } if
  47.     } //.bindscratch /IdiomSet resourceforall
  48.   } if
  49. } odef
  50. { /.bind /bind load def
  51.   /bind { } def
  52. } if
  53. currentdict /.bindscratch .undef
  54.  
  55. % ------ HalftoneTypes 6, 10, 16 and HalftoneMode ------ %
  56.  
  57. % This code depends on an internal HalftoneType 7 with the following keys:
  58. %    Width, Height, Width2, Height2, TransferFunction:
  59. %      as for HalftoneType 16.
  60. %    Thresholds: a string or bytestring holding the thresholds,
  61. %      (Width x Height + Width2 x Height2) x BitsPerSample / 8 bytes,
  62. %      as for HalftoneType 16 except that the samples may be either
  63. %      8 or 16 bits wide.
  64. %    BitsPerSample: 8 or 16.
  65.  
  66. % Note that this HalftoneType never appears in halftone dictionaries given
  67. % to sethalftone, only as a component in those given to .sethalftone5,
  68. % so its numeric value can be chosen ad lib as long as it differs from the
  69. % other values that are legal in component dictionaries for .sethalftone5
  70. % (currently only 1 and 3).
  71.  
  72. /.makehalftone7 {    % <dict> <dict> <source> <Width> <Height>
  73.             %   (<Width2> <Height2> | null) <BPS> .makehalftone7
  74.             %   <setdict> <dict5> { .sethalftone5 }
  75.   8 dict begin
  76.   /HalftoneType 7 def
  77.   /BitsPerSample exch def
  78.   dup null eq {
  79.     pop 0
  80.   } {
  81.     /Height2 1 index def /Width2 2 index def mul
  82.   } ifelse 3 1 roll
  83.   /Height 1 index def
  84.   /Width 2 index def
  85.   mul add BitsPerSample 8 idiv mul .bigstring
  86.         % Stack: dict dict source str
  87.   dup type /stringtype eq { readstring } { .readbytestring } ifelse
  88.   not { /sethalftone load /rangecheck signalerror exit } if
  89.   readonly /Thresholds exch def
  90.   /TransferFunction .knownget { /TransferFunction exch def } if
  91.         % If the original Thresholds was a file, replace it with
  92.         % a new one.
  93.   dup /Thresholds get type /filetype eq {
  94.     dup /Thresholds [ Thresholds ] cvx 0 () .subfiledecode put
  95.   } if
  96.   mark /HalftoneType 5 /Default currentdict end .dicttomark
  97.   { .sethalftone5 }
  98. } bind def
  99.  
  100. /.bigstring {    % <size> .bigstring <string|bytestring>
  101.   dup 65400 gt { .bytestring } { string } ifelse
  102. } bind def
  103.  
  104. /.readbytestring {    % <source> <bytestring> .readbytestring
  105.             %   <bytestring> <filled>
  106.         % Note that since bytestrings don't implement getinterval,
  107.         % if filled is false, there is no way to tell how much
  108.         % was read.
  109.   true exch 0 1 2 index length 1 sub {
  110.         % Stack: source true str index
  111.     3 index read not { pop exch not exch exit } if
  112.     3 copy put pop pop
  113.   } for 3 -1 roll pop exch
  114. } bind def
  115.  
  116. /.sethalftone6 {    % <dict> <dict> .sethalftone6 <setdict> <dict5>
  117.             %   { .sethalftone5 }
  118.             % Keys: Width, Height, Thresholds, T'Function
  119.   dup /Thresholds get
  120.   1 index /Width get 2 index /Height get
  121.   null 8 .makehalftone7
  122. } bind def
  123.  
  124. /.sethalftone10 {    % <dict> <dict> .sethalftone10 <setdict> <dict5>
  125.             %   { .sethalftone5 }
  126.             % Keys: Xsquare, Ysquare, Thresholds, T'Function
  127.         % Note that this is the only one of these three HalftoneTypes
  128.         % that allows either a file or a string for Thresholds.
  129.   dup /Thresholds get dup type /stringtype eq { 0 () .subfiledecode } if
  130.   1 index /Xsquare get dup 3 index /Ysquare get dup
  131.   8 .makehalftone7
  132. } bind def
  133.  
  134. /.sethalftone16 {    % <dict> <dict> .sethalftone16 <setdict> <dict5>
  135.             %   { .sethalftone5 }
  136.             % Keys: Width, Height, Width2, Height2,
  137.             %   Thresholds, T'Function
  138.   dup /Thresholds get
  139.   1 index /Width get 2 index /Height get
  140.   3 index /Width2 .knownget {  % 2-rectangle case
  141.     4 index /Height2 get
  142.   } {            % 1-rectangle case
  143.     null
  144.   } ifelse 16 .makehalftone7
  145. } bind def
  146.  
  147. .halftonetypes begin
  148.   6 /.sethalftone6 load def
  149.   10 /.sethalftone10 load def
  150.   16 /.sethalftone16 load def
  151. end
  152.  
  153. % Redefine the halftone-setting operators to honor HalftoneMode.
  154. /setcolorscreen {
  155.   /HalftoneMode getuserparam 0 eq {
  156.     //setcolorscreen
  157.   } {
  158.     12 { pop } repeat .getdefaulthalftone
  159.     { //sethalftone }
  160.     { .setdefaulthalftone }
  161.     ifelse
  162.   } ifelse
  163. } odef
  164. /setscreen {
  165.   /HalftoneMode getuserparam 0 eq {
  166.     //setscreen
  167.   } {
  168.     pop pop pop .getdefaulthalftone
  169.     { //sethalftone }
  170.     { .setdefaulthalftone }
  171.     ifelse
  172.   } ifelse
  173. } odef
  174. /sethalftone {
  175.   /HalftoneMode getuserparam 0 eq {
  176.     //sethalftone
  177.   } {
  178.     pop .getdefaulthalftone
  179.     { //sethalftone }
  180.     { .setdefaulthalftone }
  181.     ifelse
  182.   } ifelse
  183. } odef
  184.  
  185. % ------ ImageTypes 3 and 4 (masked images) ------ %
  186.  
  187. .imagetypes
  188.   dup 3 /.image3 load put
  189.   dup 4 /.image4 load put
  190. % We also detect ImageType 103 here: it isn't worth making a separate file
  191. % just for this.
  192.   /.image3x where { pop dup 103 /.image3x load put } if
  193. pop
  194.  
  195. % ------ Functions ------ %
  196.  
  197. % Define the FunctionType resource category.
  198. /Generic /Category findresource dup maxlength 3 add dict .copydict begin
  199.   /InstanceType /integertype def
  200. /FunctionType currentdict end /Category defineresource pop
  201.  
  202. {0 2 3} { dup /FunctionType defineresource pop } forall
  203.  
  204. % ------ Smooth shading ------ %
  205.  
  206. % Define the ShadingType resource category.
  207. /Generic /Category findresource dup maxlength 3 add dict .copydict begin
  208.   /InstanceType /integertype def
  209. /ShadingType currentdict end /Category defineresource pop
  210.  
  211. systemdict /.shadingtypes mark        % not ll3dict
  212.   1 /.buildshading1 load
  213.   2 /.buildshading2 load
  214.   3 /.buildshading3 load
  215.   4 /.buildshading4 load
  216.   5 /.buildshading5 load
  217.   6 /.buildshading6 load
  218.   7 /.buildshading7 load
  219. .dicttomark put
  220.  
  221. systemdict /.reuseparamdict mark
  222.   /Intent 2
  223.   /AsyncRead false
  224.   /CloseSource true
  225. .dicttomark readonly put
  226. /.buildshading {    % <shadingdict> .buildshading <shading>
  227.         % Unfortunately, we always need to make the DataSource reusable,
  228.         % because if clipping is involved, even shfill may need to read
  229.         % the source data multiple times.  If it weren't for this,
  230.         % we would only need to create a reusable stream if the ultimate
  231.         % source of the data is a procedure (since the library can't
  232.         % suspend shading to do a procedure callout).
  233.   dup /DataSource .knownget {
  234.     dup type /filetype eq {
  235.       //.reuseparamdict /ReusableStreamDecode filter
  236.       .currentglobal 1 index gcheck .setglobal
  237.         % Stack: shdict rsdfile saveglobal
  238.       2 index dup length dict copy exch .setglobal
  239.       dup /DataSource 4 -1 roll put exch pop
  240.     } {
  241.       pop
  242.     } ifelse
  243.   } if
  244.     % The .buildshading operators use the current color space
  245.     % for ColorSpace.
  246.   dup /ShadingType get //.shadingtypes exch get
  247.   1 index /ColorSpace get setcolorspace exec
  248. } bind def
  249. systemdict /.reuseparamdict undef
  250.  
  251. /.buildpattern2 {    % <template> <matrix> .buildpattern2
  252.             %   <template> <pattern>
  253.     % We want to build the pattern without doing gsave/grestore,
  254.     % since we want it to load the CIE caches.
  255.   1 index /Shading get
  256.   mark currentcolor currentcolorspace
  257.   counttomark 4 add -3 roll mark 4 1 roll
  258.     % Stack: -mark- ..color.. cspace -mark- template matrix shadingdict
  259.   { .buildshading } stopped {
  260.     cleartomark setcolorspace setcolor pop stop
  261.   } if
  262.   .buildshadingpattern
  263.   3 -1 roll pop counttomark 1 add 2 roll setcolorspace setcolor pop
  264. } bind def
  265.  
  266. .patterntypes
  267.   2 /.buildpattern2 load put
  268.  
  269. /shfill {        % <shadingdict> shfill -
  270.     % Currently, .shfill requires that the color space
  271.     % in the pattern be the current color space.
  272.   dup gsave { .buildshading .shfill } stopped grestore { stop } if
  273.   pop
  274. } odef
  275.  
  276. % Establish an initial smoothness value that matches Adobe RIPs.
  277. 0.02 setsmoothness
  278.  
  279. % ------ UseCIEColor ------ %
  280.  
  281. % The library maintains and detects the UseCIEColor device parameter,
  282. % but it doesn't have access to the resource dictionaries.  We also
  283. % want color space substitution to work in systems without a PostScript
  284. % interpreter.  Therefore, we eagerly inform the library of changes in
  285. % the (effective) ColorSpace category that might affect the operation of
  286. % UseCIEColor.  We must notice the following events:
  287. %    1) defineresource and undefineresource of the Default ColorSpaces.
  288. %    2) restore.
  289. %    3) Changes in current VM, which cause a different set of resources
  290. %    to become visible.
  291. % #1 is rare.  #2 is handled in C code.  If checking the ColorSpace
  292. % category on #3 turns out to be expensive, we can cache more information
  293. % about whether these operations actually affect UseCIEColor.
  294.  
  295. % This operator implements color space substitution in the library:
  296. %    <index> <bool> .setsubstitutecolorspace -
  297. % <bool> = true means substitute the current color space for the one given
  298. % by <index>; <bool> = false means stop substituting.  Substitution is not
  299. % affected by grestore/setgstate, but it is affected by restore.
  300.  
  301. % ColorSpace defineresource and undefineresource for the Default keys
  302. % call .definedefaultcs and .undefinedefaultcs.  See gs_res.ps.
  303.  
  304. /.useciecolorkeydict mark
  305.   /UseCIEColor null
  306. .dicttomark readonly def
  307. /.definedefaultcs {    % <index> <value> .definedefaultcs -
  308.   currentcolorspace
  309.     % Temporarily disable color substitution, in case the substitute
  310.     % color space is or mentions a color space that is currently
  311.     %  being substituted.
  312.   currentdevice //.useciecolorkeydict .getdeviceparams exch pop exch pop
  313.   mark 5 -2 roll
  314.     % Stack: cspace UseCIEColor mark index value
  315.    { setcolorspace true .setsubstitutecolorspace }
  316.   stopped counttomark 1 add 1 roll cleartomark
  317.     % Stack: cspace UseCIEColor stopped?
  318.   3 1 roll
  319.   currentdevice null true mark /UseCIEColor 6 -1 roll .putdeviceparams pop pop
  320.   setcolorspace { stop } if
  321. } bind def
  322. currentdict /.useciecolorkeydict undef
  323.  
  324. /.undefinedefaultcs {    % <index> .undefinedefaultcs -
  325.   false .setsubstitutecolorspace
  326. } bind def
  327.  
  328. /.setdefaultcs {    % <index> <value|null> .setdefaultcs -
  329.   dup null eq { pop .undefinedefaultcs } { .definedefaultcs } ifelse
  330. } bind def
  331.  
  332. /.getdefaultcs {    % <key> .getdefaultcs <value|null>
  333.   .GetInstance { 0 get } { null } ifelse
  334. } bind def
  335.  
  336. /.setglobal {        % <bool> .setglobal -
  337.   dup .currentglobal ne {
  338.     % If there are no local definitions of the Default keys,
  339.     % changing VM can't affect the definition of any resource.
  340.     /.localcsdefaults .uservar {
  341.     % We only want to change substitutions for color spaces
  342.     % whose definitions are actually changing.
  343.       /ColorSpace /Category findresource begin
  344.       /DefaultGray .getdefaultcs
  345.       /DefaultRGB .getdefaultcs
  346.       /DefaultCMYK .getdefaultcs
  347.       end
  348.       3 index .setglobal
  349.       /ColorSpace /Category findresource begin
  350.       /DefaultGray .getdefaultcs
  351.       /DefaultRGB .getdefaultcs
  352.       /DefaultCMYK .getdefaultcs
  353.       end
  354.             % Stack: bool oldgray oldrgb oldcmyk
  355.             %   newgray newrgb newcmyk
  356.       dup 4 index ne { 2 exch .setdefaultcs } { pop } ifelse
  357.       dup 4 index ne { 1 exch .setdefaultcs } { pop } ifelse
  358.       dup 4 index ne { 0 exch .setdefaultcs } { pop } ifelse
  359.       pop pop pop pop
  360.     } {
  361.       .setglobal
  362.     } ifelse
  363.   } {
  364.     .setglobal
  365.   } ifelse
  366. } .bind odef        % bind in .setglobal
  367.  
  368.  
  369. % Define default device color spaces to allow substitution
  370. % on UseCIEColor == true :
  371.  
  372. /DefaultGray
  373. [ /CIEBasedA
  374.   3 dict begin
  375.      /WhitePoint [ 0.9505 1.0 1.089 ] def
  376.      /DecodeLMN  [ { 1.8 exp } { 1.8 exp } { 1.8 exp } ] def
  377.      /MatrixLMN  [ 0.4439 0.2522 0.0436 0.3341 0.6371 0.1387 0.1725 0.1107 0.9067 ] def
  378.   currentdict end
  379. ]
  380. /ColorSpace defineresource pop
  381.  
  382. /DefaultRGB
  383. [ /CIEBasedABC
  384.   3 dict begin
  385.      /WhitePoint [ 0.9505 1.0 1.089 ] def
  386.      /DecodeLMN  [ { 1.8 exp } { 1.8 exp } { 1.8 exp } ]  def
  387.      /MatrixLMN  [ 0.4439 0.2522 0.0436 0.3341 0.6371 0.1387 0.1725 0.1107 0.9067 ] def
  388.   currentdict end
  389. ]
  390. /ColorSpace defineresource pop
  391.  
  392. /DefaultCMYK
  393. [ /CIEBasedDEFG
  394.   4 dict begin
  395.      /WhitePoint [ 0.9505 1.0 1.089 ] def
  396.      /Table [ 2 2 2 2
  397.             % Trivial table :
  398.             % [
  399.             %   [ < FFFFFF 000000 FFFF00 000000>
  400.             %     < FF00FF 000000 FF0000 000000> ]
  401.             %   [ < 00FFFF 000000 00FF00 000000>
  402.             %     < 0000FF 000000 000000 000000> ]
  403.             % ]
  404.  
  405.             % AcrobatReader-compatible table :
  406.               [
  407.                 [ < FFFFFF 000000 FFFF00 000000>
  408.                   < EA007F 000000 FC000F 000000> ]
  409.  
  410.                 [ < 00A0C8 000000 008436 000000>
  411.                   < 00007C 000000 000000 000000> ]
  412.               ]
  413.             ] def
  414.      /DecodeLMN [ { 1.8 exp } { 1.8 exp } { 1.8 exp } ] def
  415.      /MatrixLMN [ 0.4439 0.2522 0.0436 0.3341 0.6371 0.1387 0.1725 0.1107 0.9067 ] def
  416.   currentdict end
  417. ]
  418. /ColorSpace defineresource pop
  419.  
  420.  
  421. % ------ DeviceN color space ------ %
  422.  
  423. % This isn't quite right, because the ColorSpaceFamily resource will exist
  424. % even with languagelevel < 3, but it's close enough.
  425.  
  426. /.setdevicenspace where {
  427.   pop colorspacedict /DeviceN {
  428.     dup 2 get setcolorspace dup
  429.     dup 1 get length        % # of components
  430.     .converttinttransform .setdevicenspace
  431.   } bind put
  432. } if
  433.  
  434. % ------ Miscellaneous ------ %
  435.  
  436. % Define additional user and system parameters.
  437. /HalftoneMode 0 .definepsuserparam
  438. /MaxSuperScreen 1016 .definepsuserparam
  439. pssystemparams begin        % read-only, so use .forcedef
  440.   /MaxDisplayAndSourceList 160000 .forcedef
  441. end
  442.  
  443. % Define the IdiomSet resource category.
  444. { /IdiomSet } {
  445.   /Generic /Category findresource dup maxlength 3 add dict .copydict begin
  446.     /InstanceType /dicttype def
  447.   currentdict end /Category defineresource pop
  448. } forall
  449.  
  450. /languagelevel 3 def
  451. % When running in LanguageLevel 3 mode, this interpreter is supposed to be
  452. % compatible with Adobe version 3010.
  453. /version (3010) readonly def
  454.  
  455. .setlanguagelevel
  456.  
  457. end            % ll3dict
  458.